<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Admin_Professions extends Admin_Controller {
	public function __construct()
	{
		parent::__construct();
		$this->lang->load('profession');
	}
	public function ajax_profession_get(){
		$professionEntity =  new CI_Profession_entity();
		$profession = $professionEntity->getProfessionByCode($_REQUEST['code']);
		echo json_encode($profession);
	}
	public function ajax_profession_list(){
		$professionEntity =  new CI_Profession_entity();
		$escortSettings = new CI_Escort_setting_entity();
		$defaultLanguageId = $escortSettings->getDefaultLanguageId();
		$professionList = $professionEntity->getProfessionListByLanguage($defaultLanguageId);
		$data['professionList'] = $professionList;
		
		$languageEntity = new CI_Language_entity();
		$languageList = $languageEntity->getLanguageList();
		$data['languageList'] = $languageList;
		
		$this->load->view('admin/professions/index', $data);
	}
	public function ajax_profession_save(){
		$professionNameList = $_REQUEST['profession'];
		$professionNameDefault = '';
		foreach ($professionNameList as $key => $value) {
			if($value != null && $value != ""){
				$professionNameDefault = $value;
				break;
			}
		}
		$result = array();
		if($professionNameDefault == ''){
			$result['result'] = false;
			$result['message'] = lang('escort_profession_require');
		}else{
			if($this->_check_profession(0, $professionNameDefault)){
				$result['result'] = false;
				$result['message'] = lang('escort_profession_existed');
			}else{
				$code = 0;
				foreach ($professionNameList as $key => $value) {
					$professionEntity =  new CI_Profession_entity();
					if($value != null && $value != ''){
						$professionEntity->setProfession($value);
					}else{
						$professionEntity->setProfession($professionNameDefault);
					}
					$professionEntity->setLanguageId($key);
					$professionEntity->setCode($code);
					$id = $professionEntity->save();
					if($code == 0){
						$code = $id;
						$professionEntity->setId($id);
						$professionEntity->setCode($code);
						$professionEntity->update();
					}
				}
				$result['result'] = true;
				$result['message'] =lang('escort_profession_save_success');
				$result['id'] =$id;
			}
		}
		echo json_encode($result);
	}
	public function ajax_profession_update(){
		$professionNameList = $_REQUEST['profession'];
		$professionIdList = $_REQUEST['profession_id'];
		$professionCode = $_REQUEST['profession_code'];
		$professionNameDefault = '';
		foreach ($professionNameList as $key => $value) {
			if($value != null && $value != ""){
				$professionNameDefault = $value;
				break;
			}
		}
		$result = array();
		if($professionNameDefault == ''){
			$result['result'] = false;
			$result['message'] = lang('escort_profession_require');
		}else{
			$keys = array_keys($professionIdList);
			/*
			if($this->_check_profession($professionIdList[$keys[0]], $professionNameDefault)){
				
				$result['result'] = false;
				$result['message'] = lang('escort_profession_existed');
			}else{
			*/
				foreach ($professionNameList as $key => $value) {
					$professionEntity =  new CI_Profession_entity();
					if($value != null && $value != ''){
						$professionEntity->setProfession($value);
					}else{
						$professionEntity->setProfession($professionNameDefault);
					}
					$professionEntity->setLanguageId($key);
					$professionEntity->setCode($professionCode);
					$professionEntity->setId($professionIdList[$key]);
					$professionEntity->update();
				}
				$result['result'] = true;
				$result['message'] =lang('escort_profession_save_success');
				/*
			}
			*/
		}
		
		echo json_encode($result);
	}
	public function ajax_profession_delete(){
		$result = array();
		$professionEntity =  new CI_Profession_entity();
		$result['result'] = $professionEntity->deleteByCode($_REQUEST['code']);
		if($result['result']){
			$result['message'] = lang('escort_profession_delete_success');
		}else{
			$result['message'] = lang('escort_profession_delete_error');
		}
		echo json_encode($result);
	}
	function _check_profession($id, $name){
		$ci = &get_instance();
		return $ci->profession_entity->checkExistedName($id, $name);
	}
}
?>